package com.mystore.servlet;

import com.mystore.entity.User;
import com.mystore.exception.UserException;
import com.mystore.service.CartService;
import com.mystore.service.UserService;
import com.mystore.service.impl.CartServiceImpl;
import com.mystore.service.impl.UserServiceImpl;
import com.mystore.util.CartUtil;
import com.mystore.vo.GoodVO;
import com.mystore.vo.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "UserServlet",value = "/uncheck/user/*")
public class UserServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    CartService cartService = new CartServiceImpl();
    CartUtil cartUtil =CartUtil.getInstance();
    public static final String USER = "user";
    public static final String CART = "cartList";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String uri = request.getRequestURI();
       String method = uri.replace(request.getContextPath() + "/uncheck/user/", "");
       System.out.println("method="+method);
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();

        String url = request.getContextPath();
        System.out.println("url="+url);

        switch (method){
            case "login":
                //用户登录
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                UserVO userVO = new UserVO(username,password);
                try {
                    Object obj = userService.isLogin(userVO);

                    if (obj == null) {
                        url = "/login.jsp";
                        request.setAttribute("msg", "用户名或密码错误,请重新登录");
                        request.getRequestDispatcher(url).forward(request, response);
                    } else {
                        System.out.println("login okay");
                        out.println("登录成功,即将跳往首页");
                        User user = (User)obj;
                        session.setAttribute(UserServlet.USER,user);

                        List<GoodVO> list = cartService.findMyCart(user.getId());
                        for(GoodVO goodVO:list){
                            cartUtil.addCart(goodVO.getGid(),goodVO );
                        }
                        //设置购物车
                        session.setAttribute(CART,cartUtil);

                        url += "/check/kind/getAllKind";
                        response.setHeader("refresh", "2;url=" + url);
                    }
                }catch (UserException e){
                    System.out.println("usr not found");
                    response.sendRedirect(url+"/confirm.jsp");
                }
                break;

            case "reg":
                //用户注册
                String username1 = request.getParameter("username");
                String password1 = request.getParameter("password");
                UserVO userVO1 = new UserVO(username1,password1);
                try {
                    Boolean isadd= userService.addUser(userVO1);
                    if(isadd){
                        out.println("注册成功,即将跳往登录页面");
                        url+="/login.jsp";
                    }else{
                        out.println("注册失败,请重新注册");
                        url+="/reg.jsp";
                    }
                    //设置两秒后进行跳转
                    response.setHeader("refresh","2;url="+url);
                }catch (UserException e){
                    out.println("注册失败,当前用户已存在,请重新注册");
                    url+="/reg.jsp";
                    response.setHeader("refresh","2;url="+url);
                }
                break;

            default:
                    out.println("invalid mark");

        }





    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
